package jp.sourceforge.akjrcp.flvtools.basic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class FLVUtil {
public static boolean isFLV(File file){
	if(file.isDirectory()){
		return false;
	}
	if(getExtension(file.getName()).toLowerCase().equals("flv")){
		return true;
	}
	if(getExtension(file.getName()).equals("") ){
		return isFLVFileFormat(file);
	}
	return false;
}
public static String getExtension(String name){
	int ex=name.lastIndexOf(".");
	if(ex==-1){
		return "";
	}else{
		return name.substring(ex+1);
	}
}
public static boolean isFLVFileFormat(File file){
		try {
			InputStream input=new FileInputStream(file);
			byte[] bts=new byte[5];
			int readed=input.read(bts);
			if(readed<5){
				return false;
			}
			input.close();
			
			if(bts[0]=='F' && bts[1]=='L' && bts[2]=='V'){
				
				if(bts[3]>=10){ 
					return false;
					//this method will work until FLV 10 released
					//0-9 control code is minor
					
				}
				
				return true;
			}
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		return false;
	}

}
